/*
 * ConfirmOrderModel.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.R;
import com.yidiandao.business.constant.Preferences;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.bean.Order;
import com.yidiandao.business.data.net.BaseRequest;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.ChangePayWaysRequest;
import com.yidiandao.business.data.request.GetPayWayListRequest;
import com.yidiandao.business.data.request.QueryMultiPayWaysResultRequest;
import com.yidiandao.business.data.request.QueryOrderResultRequest;
import com.yidiandao.business.data.request.QueryShippingAboutRequest;
import com.yidiandao.business.data.request.SubmitOrderRequest;
import com.yidiandao.business.data.response.GetPayWayListResponse;
import com.yidiandao.business.data.response.GetShippingWayListResponse;
import com.yidiandao.business.data.response.QueryConfirmOrderDataResponse;
import com.yidiandao.business.data.response.QueryConfirmOrderGoodsResponse;
import com.yidiandao.business.data.response.QueryMultiPayWaysResultResponse;
import com.yidiandao.business.data.response.QueryOrderResultResponse;
import com.yidiandao.business.data.response.QueryShippingFeeByAddressResponse;
import com.yidiandao.business.data.response.SubmitOrderResponse;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.Functional;
import com.yidiandao.business.ui.widget.listview.displaylistitem.BuyGoodsListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.BuyGoodsReduceListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ChooseShippingWayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.PayFailedListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.PayFailedTipsListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.PayOrderInfoListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.PayResultKeyValueListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.PayResultTitleListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.PaySafetyTipsListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.PaySuccessListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.PayTransferInfoListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * ConfirmOrderModel Impl
 * <p>
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/2/15 16:07
 */
public class ConfirmOrderModel implements IConfirmOrderModel {

    private NetBiz mNetBiz = new NetBiz();

    private List<DisplayListItem> mPayResultListItems = new ArrayList<>();

    @Override
    public void loadInventory(String tag, final OnInventoryListener listener) {

        BaseRequest request = new BaseRequest();

        request.setUrl(Urls.CONFIRM_ORDER_GOODS_LIST);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);

        mNetBiz.sendPostRequest(request, QueryConfirmOrderGoodsResponse.class, new NetListener<QueryConfirmOrderGoodsResponse>() {
            @Override
            public void onSuccess(QueryConfirmOrderGoodsResponse response) {

                List<DisplayListItem> displayListItems = new ArrayList<>();

                QueryConfirmOrderGoodsResponse.Data data = response.getData();
                List<QueryConfirmOrderGoodsResponse.Inventory> inventories = data.getInventory();

                for (QueryConfirmOrderGoodsResponse.Inventory inventory : inventories) {

                    List<Goods> goodsList = inventory.getGoodsList();
                    List<QueryConfirmOrderGoodsResponse.Favourable> favourableList = inventory.getFavourableList();

                    for (Goods goods : goodsList) {
                        displayListItems.add(new BuyGoodsListItem(goods));
                    }

                    for (QueryConfirmOrderGoodsResponse.Favourable favourable : favourableList) {
                        displayListItems.add(new BuyGoodsReduceListItem(favourable));
                    }
                }

                if (listener != null) {
                    listener.onSuccess(displayListItems, response.getData().getGoodsTotalNumber());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void loadOthersConfirmOrderData(String tag, final OnConfirmOrderResponseListener listener) {
        BaseRequest request = new BaseRequest();
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.BALANCE_CART);
        request.setTag(tag);

        mNetBiz.sendPostRequest(request, QueryConfirmOrderDataResponse.class, new NetListener<QueryConfirmOrderDataResponse>() {
            @Override
            public void onSuccess(QueryConfirmOrderDataResponse response) {
                if (listener != null) {
                    listener.onSuccess(response);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void getShippingInfo(String pagerTag, String addressId, final OnLoadListItemsListener listener) {

        QueryShippingAboutRequest request = new QueryShippingAboutRequest();
        request.setUrl(Urls.GET_CARRIAGE_INFO);
        request.setTag(pagerTag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setAddressId(addressId);

        mNetBiz.sendPostRequest(
                request,
                GetShippingWayListResponse.class,
                new NetListener<GetShippingWayListResponse>() {
                    @Override
                    public void onSuccess(GetShippingWayListResponse response) {

                        GetShippingWayListResponse.Data data = response.getData();
                        List<GetShippingWayListResponse.ShippingWayInfo> shippingWayInfoList =
                                data.getShippingWayInfoList();

                        List<DisplayListItem> displayListItems = Functional.serverToClient(shippingWayInfoList,
                                new Functional.Processor<GetShippingWayListResponse.ShippingWayInfo, DisplayListItem>() {
                                    @Override
                                    public DisplayListItem processToClient(GetShippingWayListResponse.ShippingWayInfo carriageInfo) {
                                        return new ChooseShippingWayListItem(carriageInfo);
                                    }
                                });

                        if (listener != null) {
                            listener.onSuccess(displayListItems);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return false;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void updateShippingFeeByAddress(
            String tag,
            String addressId,
            String recJson,
            String favourableJson,
            boolean isHomeActivityGoods,
            String goodsId,
            int goodsNumber,
            final OnQueryShippingFeeByAddressListener listener) {

        QueryShippingAboutRequest request = new QueryShippingAboutRequest();
        request.setUrl(Urls.GET_SHIPPING_FEE_BY_ADDRESS);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setAddressId(addressId);

        if (isHomeActivityGoods) {
            request.setGoodsId(goodsId);
            request.setGoodsNumber(goodsNumber + "");
        } else {
            request.setRecJson(recJson);
            request.setFavourableJson(favourableJson);
        }

        mNetBiz.sendPostRequest(request, QueryShippingFeeByAddressResponse.class, new NetListener<QueryShippingFeeByAddressResponse>() {
            @Override
            public void onSuccess(QueryShippingFeeByAddressResponse response) {
                if (listener != null) {
                    listener.onSuccess(response.getData().getShopList());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void submitOrder(String tag, String message, final OnSubmitOrderListener listener) {

        SubmitOrderRequest request = new SubmitOrderRequest();

        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setUrl(Urls.SUBMIT_ORDER);
        request.setMessage(message);

        mNetBiz.sendPostRequest(request, SubmitOrderResponse.class, new NetListener<SubmitOrderResponse>() {
            @Override
            public void onSuccess(SubmitOrderResponse response) {
                if (listener != null) {
                    listener.onSuccess(response);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void setNeedRefreshShoppingCart(boolean refreshShoppingCart) {
        PreferenceUtils.setBoolean(UIUtils.getContext(), Preferences.REFRESH_SHOPPING_CART, refreshShoppingCart, true);
    }

    @Override
    public void loadPayResult(
            final String orderPayId,
            String orderId,
            String tag,
            final OnPayResultListener listener) {

        QueryOrderResultRequest request = new QueryOrderResultRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.ORDER_SUBMIT_RESULT);
        request.setOrderPayId(orderPayId);
        request.setOrderId(orderId);

        mNetBiz.sendGetRequest(request, QueryOrderResultResponse.class, new NetListener<QueryOrderResultResponse>() {
            @Override
            public void onSuccess(QueryOrderResultResponse response) {

                mPayResultListItems.clear();

                QueryOrderResultResponse.Data data = response.getData();

                if (data.getPayStatus() == QueryOrderResultResponse.PAY_SUCCESS) {
                    // 支付成功
                    mPayResultListItems.add(new PaySuccessListItem(data));

                    List<Order> orderList = data.getOrderList();
                    for (int i = 0; i < orderList.size(); i++) {
                        Order order = orderList.get(i);
                        if (i == 0) {
                            // 第一个添加收货人
                            order.setConsignee(data.getConsignee());
                        }
                        if (i == orderList.size() - 1) {
                            // 最后一个添加地址
                            order.setAddress(data.getAddress());
                        }
                        mPayResultListItems.add(new PayOrderInfoListItem(order));
                    }

                    // 有银行转账信息
                    if (data.getPayDesc() != null && data.getPayDesc().size() != 0) {
                        mPayResultListItems.add(new PayResultTitleListItem(UIUtils.getString(R.string.bank_transfer_info)));
                        for (QueryOrderResultResponse.Data.PayDesc payDesc : data.getPayDesc()) {
                            mPayResultListItems.add(new PayResultKeyValueListItem(payDesc));
                        }
                        mPayResultListItems.add(new PayTransferInfoListItem(data));
                    }

                } else {
                    // 支付失败
                    mPayResultListItems.add(new PayFailedListItem(data));
                    mPayResultListItems.add(new PayFailedTipsListItem(null));
                }

                // 安全提示
                mPayResultListItems.add(new PaySafetyTipsListItem(null));

                if (listener != null) {
                    listener.onSuccess(mPayResultListItems);
                    listener.onPayResult(data.getShare(), data.getPayTitle(), data.getPayStatus() == QueryOrderResultResponse.PAY_SUCCESS);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void getPayWayList(String tag, final OnLoadPayWayListListener listener) {

        GetPayWayListRequest request = new GetPayWayListRequest();
        request.setUrl(Urls.GET_PAY_WAY_LIST);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
//        request.setRecJson(recJson);
//        request.setFavourableJson(favourableJson);

        mNetBiz.sendPostRequest(request, GetPayWayListResponse.class, new NetListener<GetPayWayListResponse>() {
            @Override
            public void onSuccess(GetPayWayListResponse response) {
                if (listener != null) {
                    listener.onSuccess(response.getData().getShopPayWays(), response.getData().getOnlinePayLimit());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void changePayWays(String tag, String payWaysJson, final OnChoosePayWaysListener listener) {

        ChangePayWaysRequest request = new ChangePayWaysRequest();
        request.setTag(tag);
        request.setPaymentJson(payWaysJson);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.ChANGE_PAY_WAYS);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return true;
            }

            @Override
            public void onFinal() {

            }
        });

    }

    @Override
    public void loadMultiPayWaysResult(String tag, String orderPayId, final OnMultiPayWayResultListener listener) {

        QueryMultiPayWaysResultRequest request = new QueryMultiPayWaysResultRequest();
        request.setUrl(Urls.MULTI_PAY_WAYS);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setOrderPyaId(orderPayId);

        mNetBiz.sendGetRequest(request, QueryMultiPayWaysResultResponse.class, new NetListener<QueryMultiPayWaysResultResponse>() {
            @Override
            public void onSuccess(QueryMultiPayWaysResultResponse response) {
                if (listener != null) {
                    listener.onSuccess(response.getData());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public void onFinal() {

            }
        });

    }
}
